﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using ZLCMS.Business;
using System.Web.Configuration;
using DotNet.Common.Command;
using ZLCMS.Business.Util;
using DotNet.Common.DataTransfers;
using DotNet.Common.DataTransfers.Config;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Coupon
{
    public partial class MemberImport : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
        //    _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            if (!IsPostBack)
            {
                ViewState["RuleId"] = Request["uid"];
            }
            else
            {
                string exl = Server.MapPath("~/Upload/" + DateTime.Now.Ticks + ".xls");
                FileUpload1.SaveAs(exl);
                ViewState["file"] = exl;

                Install();
            }
        }

        public string ConnectionString
        {
            get
            {
                return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ViewState["file"] + ";Extended Properties=Excel 12.0 Xml;";
            }
        }
        protected void ButtonImport_Click(object sender, EventArgs e)
        {
         //   Literal1.Text = "";
            string exl = Server.MapPath("~/Upload/" + DateTime.Now.Ticks + ".xls");
            FileUpload1.SaveAs(exl);
            ViewState["file"] = exl;


            Install();
        }

        protected void Install()
        {
            int ruleId=int.Parse(ViewState["RuleId"]==null?"0":ViewState["RuleId"].ToString());
            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IList<ShopCouponTicket> ticketList = session.CreateQuery("from ShopCouponTicket where RuleId=:rid and Status='0' ").SetParameter("rid", ruleId).List<ShopCouponTicket>();

            ExcelLoaderUtil loader = new ExcelLoaderUtil();
            loader.ConnectionString = ConnectionString;

            IList<IDictionary> list = loader.LoadSource("");
            if (list.Count > ticketList.Count)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-参数错误" });
                lbl_msg.InnerText = "优惠券不足";
            }
            else
            {
                for (int i = 0; i < list.Count;i++ )
                {
                    ticketList[i].Status = "1";
                    ticketList[i].MemberAccount = list[i]["MemberAccount"].ToString();
                    session.Update(ticketList[i]);
                }
                session.Flush();
                Response.Write("<script type='text/javascript'>alert('导入成功！'); window.close(); </script>");
                lbl_msg.InnerText = "操作成功";
              //  _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功" });
            }

            //foreach (IDictionary dic in list)
            //{
             
            //}
            //TargetInstaller installer = new TargetInstaller();
            //TupleInstallElement element = TransfersConfig.Configuare(Server.MapPath("~/App_Data/import/score_card_transfer.xml"));

            //int count = installer.Install(list, element);
            //string tips = "<p>{2}_处理结果——待处理记录{0}条，已影响记录{1}条</p>";
            //Literal1.Text += string.Format(tips, list.Count, count, element.Description);
        }}
    }
}